<?php
namespace Admin\Model;
use Think\Model;
class OrderModel extends Model {
	public $table='zjy_order';
	public $page; //当前页
	public $perpage; //每页显示
	public $order_id; //ID
	public $post; //提交的信息 数组
	public $ids; //IDs 主要用于删除
	public $sorts; //IDs 主要用于删除
	public $keyword; //关键词
	public $data; //传递的数组数据

	//获得列表 $page当前页 $perpage每页显示 当前默认用户名 姓名
	public function get_order_list()
	{
		$page=$this->page;
		$perpage=$this->perpage;
		//搜索
		$where='where 1 ';
		if($this->keyword){
			$where.="and `order_sn` LIKE '%".$this->keyword."%'";
		}
		/*
		if($this->pc_id){
			$pc_ids=$this->get_sub_pc_id($this->pc_id);
			$pc_ids=mb_strcut($pc_ids,1); //裁掉第一个，
			$where.="and c.pc_id in(".$pc_ids.")";
		}*/
		$start=($page-1)*$perpage;
		$Model=new Model();
		$Member=M("Member");
		$list=$Model->query("select * from ".$this->table." ".$where." order by order_id desc limit $start,$perpage");
		foreach($list as $k=>$v){
			$list[$k]['seller_name']=$Member->where('member_id='.$v['seller_id'])->getField('member_truename');
			$list[$k]['buyer_name']=$Member->where('member_id='.$v['buyer_id'])->getField('member_truename');
		}
		return $list;
	}
	//获得总数
	public function get_order_total()
	{
		//搜索
		$where='where 1 ';
		if($this->keyword){
			$where.="and `order_sn` LIKE '%".$this->keyword."%'";
		}
		/*if($this->pc_id){
			$pc_ids=$this->get_sub_pc_id($this->pc_id);
			$pc_ids=mb_strcut($pc_ids,1); //裁掉第一个，
			$where.="and pc_id in(".$pc_ids.")";
		}*/
		$Model=new Model();
		$query=$Model->query("select count(order_id) as total from ".$this->table." ".$where." ");
		return $query[0]['total'];
	}
	//获取信息
	public function get_order_info()
	{
		$order_id=intval($this->order_id);
		$Model=M("Order");
		$Member=M("Member");
		$OrderProduct=M("Order_product");
		$OrderAddress=M("Order_address");
		$Match=M("Match");
		$Purchase=M("Purchase");
		$Supply=M("Supply");
		$info=$Model->where('order_id='.$order_id)->find();
		$info['match']=$Match->where('match_id='.$info['match_id'])->find();
		$info['purchase_title']=$Purchase->where('purchase_id='.$info['match']['purchase_id'])->getField('purchase_title');
		$info['supply_title']=$Supply->where('supply_id='.$info['match']['supply_id'])->getField('supply_title');
		//print_r($info);exit;
		$info['seller_name']=$Member->where('member_id='.$info['seller_id'])->getField('member_truename');
		$info['buyer_name']=$Member->where('member_id='.$info['buyer_id'])->getField('member_truename');
		$info['product_list']=$OrderProduct->where('order_id='.$order_id)->order('rec_id asc')->select();
		$info['order_address']=$OrderAddress->where('order_id='.$order_id)->find();
		//print_r($info['order_address']);exit;
		return $info;
	}
	//编辑提交
	public function order_edit_post()
	{
		$Model=M("Order");
		$OrderAddress=M("Order_address");
		$condition['order_id'] = $this->post['order_id'];
		$data=$this->post;
		//print_r($this->post);exit;
		$result=$Model->where($condition)->save($data);
		$result2=$OrderAddress->where($condition)->save($data);
		if($result===false||$result2===false) {
			return false;
		}else{
			return true;
		}
	}
	//删除
	public function delete_order()
	{
		$ids=$this->ids;
		$Model=M("Order");
		foreach($ids as $v){
			$condition['order_id']=$v;
			if(!$Model->where($condition)->delete()){
				return false;
			}
		}
		return true;
	}
	//检测 不存在则写入订单
	public function check_add_order(){
		$Model=M("Order");
		//echo $this->match_id;
		//查询是否已经存在
		$num=$Model->where('match_id='.$this->data['match_id'])->count();
		//echo $num;exit;
		//print_r($this->data);exit;
		if($num==0){
			//写入
			$data=$this->data;
			if(!$Model->add($data)){
				return false;
			}
		}
		return true;
	}

}